﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace App1
{
    class Pieces
    {
        protected int _row;

        public int Row
        {
            get { return _row; }
            set { _row = value; }
        }
        protected int _col;

        public int Col
        {
            get { return _col; }
            set { _col = value; }
        }
        protected string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        protected int _value;

        public int Value
        {
            get { return _value; }
            set { _value = value; }
        }
        protected int _side;

        public int Side
        {
            get { return _side; }
            set { _side = value; }
        }
        protected bool _state;

        public bool State
        {
            get { return _state; }
            set { _state = value; }
        }
        public PictureBox pic = new PictureBox();
        protected bool _lock;

        public bool Lock
        {
            get { return _lock; }
            set { _lock = value; }
        }

        public Pieces()
        {
            _row = -1;
            _col = -1;
            _name = "";
            _side = 0;
            _state = false;
            _lock = true;
            pic.Width = 57;
            pic.Height = 57;
            pic.Cursor = Cursors.Hand;
            pic.Top = _row * 63;
            pic.Left = _col * 63;
            pic.BackColor = Color.Transparent;
        }

        public void Init(int row, int col, string name, int value, int side, bool state, bool lk)
        {
            _row = row;
            _col = col;
            _name = name;
            _value = value;
            _side = side;
            _state = state;
            _lock = lk;
            pic.Width = 57;
            pic.Height = 57;
            pic.Cursor = Cursors.Hand;
            pic.Top = _row * 63;
            pic.Left = _col * 63;
            pic.BackColor = Color.Transparent;
            pic.MouseClick += new MouseEventHandler(_pic_MouseClick);
        }

        public void SetImageMark()
        {
            if (_side == 1)
                {
                    switch (_value)
                    {
                        case 1:
                            pic.Image = Properties.Resources.aPawnMark;
                            break;
                        case 2:
                            pic.Image = Properties.Resources.aGuardMark;
                            break;
                        case 3:
                            pic.Image = Properties.Resources.aElephantMark;
                            break;
                        case 4:
                            pic.Image = Properties.Resources.aHorseMark;
                            break;
                        case 5:
                            pic.Image = Properties.Resources.aCannonMark;
                            break;
                        case 6:
                            pic.Image = Properties.Resources.aRookMark;
                            break;
                        case 7:
                            pic.Image = Properties.Resources.aKingMark;
                            break;
                    }
                }
            if (_side == 2)
            {
                switch (_value)
                {
                    case 1:
                        pic.Image = Properties.Resources.bPawnMark;
                        break;
                    case 2:
                        pic.Image = Properties.Resources.bGuardMark;
                        break;
                    case 3:
                        pic.Image = Properties.Resources.bElephantMark;
                        break;
                    case 4:
                        pic.Image = Properties.Resources.bHorseMark;
                        break;
                    case 5:
                        pic.Image = Properties.Resources.bCannonMark;
                        break;
                    case 6:
                        pic.Image = Properties.Resources.bRookMark;
                        break;
                    case 7:
                        pic.Image = Properties.Resources.bKingMark;
                        break;
                }
            }
        }

        private void _pic_MouseClick(Object sender, MouseEventArgs e)
        {
            if (GameChess.Marked)
            {
                if (_state)
                {
                    if (_side != GameChess.PieceMark._side)
                    {
                        if (GameChess.PieceMark.CheckLegalMove(_row, _col) && GameChess.PieceMark.CheckSafeOfKing(_row, _col))
                        {
                            GameChess.SaveToLogMove(this);
                            GameChess.KillPiece(this);
                            GameChess.MakeEmptySquare(GameChess.PieceMark._row, GameChess.PieceMark._col);
                            GameChess.SetPieceOnBoard(GameChess.PieceMark, _row, _col);
                            GameChess.CheckCheck();
                            GameChess.ChangeTurn();

                            if (GameChess.Winner != 0)
                            {
                                GameChess.picCheck.Visible = false;
                                GameChess.picStalemate.Visible = true;
                            }
                            else
                                GameChess.picStalemate.Visible = false;
                            //Board.UpdatePicCanMove();
                        }

                        GameChess.Marked = false;
                        GameChess.PieceMark.SetImageMark();
                        Board.UpdatePicCanMove();
                    }
                }
            }
            else
            {
                if (_state)
                {
                    if (!_lock)
                    {
                        GameChess.Marked = true;
                        GameChess.PieceMark = new Pieces();
                        GameChess.PieceMark = this;

                        this.SetImageMark();

                        for(int row = 0; row < 10; ++row)
                            for (int col = 0; col < 9; ++col)
                                if (row != _row || col != _col)
                                    if (this.CheckLegalMove(row, col) && this.CheckSafeOfKing(row, col))
                                        Board.Square[row, col].PicCanMove.Visible = true;
                    }
                }
            }
        }

        public void SetImage()
        {
            if (_side == 1)
            {
                switch(_value)
                {
                    case 1:
                        pic.Image = Properties.Resources.aPawn;
                        break;
                    case 2:
                        pic.Image = Properties.Resources.aGuard;
                        break;
                    case 3:
                        pic.Image = Properties.Resources.aElephant;
                        break;
                    case 4:
                        pic.Image = Properties.Resources.aHorse;
                        break;
                    case 5:
                        pic.Image = Properties.Resources.aCannon;
                        break;
                    case 6:
                        pic.Image = Properties.Resources.aRook;
                        break;
                    case 7:
                        pic.Image = Properties.Resources.aKing;
                        break;
                }
            }
            else if (_side == 2)
            {
                switch (_value)
                {
                    case 1:
                        pic.Image = Properties.Resources.bPawn;
                        break;
                    case 2:
                        pic.Image = Properties.Resources.bGuard;
                        break;
                    case 3:
                        pic.Image = Properties.Resources.bElephant;
                        break;
                    case 4:
                        pic.Image = Properties.Resources.bHorse;
                        break;
                    case 5:
                        pic.Image = Properties.Resources.bCannon;
                        break;
                    case 6:
                        pic.Image = Properties.Resources.bRook;
                        break;
                    case 7:
                        pic.Image = Properties.Resources.bKing;
                        break;
                }
            }

            Board.Init(_row, _col, _name, _value, _side, true);
        }
        public virtual bool CheckLegalMove(int row, int col)
        {
            return false;
        }
        public virtual bool CheckSafeOfKing(int row, int col)
        {
            return false;
        }

        public void OpenRow(int r)
        {
            _row = r;
        }

        public void OpenCol(int c)
        {
            _col = c;
        }


        public int SaveRow()
        {
            return _row;
        }

        public int SaveCol()
        {
            return _col;
        }
    }
}
